package com.antoco.controlcenter.comm;

import com.antoco.controlcenter.comm.message.RobotMessageBase;
import com.antoco.controlcenter.utils.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class PropertyManager {
    private ConcurrentMap<String, PropertyBase> propertyMap;//属性列表

    public PropertyManager(){
        this.propertyMap = new ConcurrentHashMap<>();
    }

    /**
     * 获取属性
     * @param key
     * @return
     */
    public PropertyBase getProperty(String key) {
        return propertyMap.get(key);
    }

    /**
     * 设置属性
     * @param property
     */
    public void setProperty(PropertyBase property) {
        this.propertyMap.put(property.getName(),property);
    }

    /**
     * 设置一批属性
     * @param propertyList
     */
    public void setProperties(Collection<PropertyBase> propertyList) {
        for(PropertyBase property : propertyList){
            this.propertyMap.put(property.getName(),property);
        }
    }

    /**
     * 设置属性值
     * @param key
     * @param value
     */
    public void setPropertieValue(String key,Object value){
        if(propertyMap.containsKey(key)){
            propertyMap.get(key).setValue(value);
        }
    }

    public void clear(){
        this.propertyMap.clear();
    }

    public void setPropertieNumberValue(String key,double value){
        if(propertyMap.containsKey(key)){
            propertyMap.get(key).setNumberValue(value);
        }
    }
    /**
     * 由消息解析所有属性值
     * @param message
     */
    public void parserMessage(RobotMessageBase message){
        byte[] buffer = message.GetDataBuffer();
        for(PropertyBase property : propertyMap.values()){
            if(property.getIsParser()){ //需要解析的参数
                property.parserValue(buffer);
            }
        }
    }

    /**
     * 获取所有显示参数
     * @return
     */
    public List<String> getAllDisplay(){
        List<String> stringList = new ArrayList<>();
        for(PropertyBase property : propertyMap.values()){
            if(property.getIsDisplay()){ //需要解析的参数
                String display = property.getDisplay();
                if(!StringUtils.isEmpty(display)){
                    stringList.add(display);
                }
            }
        }
        return stringList;
    }


}
